<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python数据分析课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>ndarray的创建与数据类型 | Python数据分析课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part02/2.2.html" />
    
    
    <link rel="prev" href="../../file/part02/2.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.1"
        data-chapter-title="ndarray的创建与数据类型"
        data-filepath="file/part02/2.1.md"
        data-basepath="../.."
        data-revision="Thu Apr 27 2017 00:50:19 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        一、工作环境准备及数据分析建模理论基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Python 3.x新特性和编码回顾
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        DIKW模型与数据工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        数据分析建模理论基础
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        二、科学计算工具NumPy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        ndarray的创建与数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        ndarray的矩阵处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        ndarray的元素处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        实战案例：2016美国总统大选民意调查统计
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        三、数据分析工具Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Pandas的数据结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Pandas的索引操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Pandas的对齐运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Pandas的函数应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        Pandas的层级索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        Pandas统计计算和描述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        Pandas分组与聚合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        数据清洗、合并、转化和重构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        聚类模型 -- K-Means介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="file/part03/3.10.html">
            
                
                    <a href="../../file/part03/3.10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        实战案例：全球食品数据分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        四、数据可视化工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Matplotlib绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        Seaborn绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Bokeh绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        实战案例：世界高峰数据可视化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        五、自然语言处理NLTK
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        NLTK与自然语言处理基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        jieba分词
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        情感分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        文本相似度和分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        实战案例：微博情感分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python数据分析课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="ndarray-&#x591A;&#x7EF4;&#x6570;&#x7EC4;n-dimension-array">ndarray &#x591A;&#x7EF4;&#x6570;&#x7EC4;(N Dimension Array)</h1>
<h6 id="numpy&#x6570;&#x7EC4;&#x662F;&#x4E00;&#x4E2A;&#x591A;&#x7EF4;&#x7684;&#x6570;&#x7EC4;&#x5BF9;&#x8C61;&#xFF08;&#x77E9;&#x9635;&#xFF09;&#xFF0C;&#x79F0;&#x4E3A;ndarray&#xFF0C;&#x5177;&#x6709;&#x77E2;&#x91CF;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x80FD;&#x529B;&#x548C;&#x590D;&#x6742;&#x7684;&#x5E7F;&#x64AD;&#x80FD;&#x529B;&#xFF0C;&#x5E76;&#x5177;&#x6709;&#x6267;&#x884C;&#x901F;&#x5EA6;&#x5FEB;&#x548C;&#x8282;&#x7701;&#x7A7A;&#x95F4;&#x7684;&#x7279;&#x70B9;&#x3002;">NumPy&#x6570;&#x7EC4;&#x662F;&#x4E00;&#x4E2A;&#x591A;&#x7EF4;&#x7684;&#x6570;&#x7EC4;&#x5BF9;&#x8C61;&#xFF08;&#x77E9;&#x9635;&#xFF09;&#xFF0C;&#x79F0;&#x4E3A;<code>ndarray</code>&#xFF0C;&#x5177;&#x6709;&#x77E2;&#x91CF;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x80FD;&#x529B;&#x548C;&#x590D;&#x6742;&#x7684;&#x5E7F;&#x64AD;&#x80FD;&#x529B;&#xFF0C;&#x5E76;&#x5177;&#x6709;&#x6267;&#x884C;&#x901F;&#x5EA6;&#x5FEB;&#x548C;&#x8282;&#x7701;&#x7A7A;&#x95F4;&#x7684;&#x7279;&#x70B9;&#x3002;</h6>
<h4 id="&#x6CE8;&#x610F;&#xFF1A;ndarray&#x7684;&#x4E0B;&#x6807;&#x4ECE;0&#x5F00;&#x59CB;&#xFF0C;&#x4E14;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;&#x5FC5;&#x987B;&#x662F;&#x76F8;&#x540C;&#x7C7B;&#x578B;"><strong>&#x6CE8;&#x610F;&#xFF1A;ndarray&#x7684;&#x4E0B;&#x6807;&#x4ECE;0&#x5F00;&#x59CB;&#xFF0C;&#x4E14;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;&#x5FC5;&#x987B;&#x662F;&#x76F8;&#x540C;&#x7C7B;&#x578B;</strong></h4>
<h4 id="ndarray&#x62E5;&#x6709;&#x7684;&#x5C5E;&#x6027;">ndarray&#x62E5;&#x6709;&#x7684;&#x5C5E;&#x6027;</h4>
<ol>
<li><code>ndim&#x5C5E;&#x6027;</code>&#xFF1A;&#x7EF4;&#x5EA6;&#x4E2A;&#x6570;</li>
<li><code>shape&#x5C5E;&#x6027;</code>&#xFF1A;&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;</li>
<li><code>dtype&#x5C5E;&#x6027;</code>&#xFF1A;&#x6570;&#x636E;&#x7C7B;&#x578B;</li>
</ol>
<blockquote>
<h2 id="ndarray&#x7684;&#x968F;&#x673A;&#x521B;&#x5EFA;">ndarray&#x7684;&#x968F;&#x673A;&#x521B;&#x5EFA;</h2>
<p>&#x901A;&#x8FC7;&#x968F;&#x673A;&#x62BD;&#x6837; (numpy.random) &#x751F;&#x6210;&#x968F;&#x673A;&#x6570;&#x636E;&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5BFC;&#x5165;numpy&#xFF0C;&#x522B;&#x540D;np</span>
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># &#x751F;&#x6210;&#x6307;&#x5B9A;&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;&#xFF08;3&#x884C;4&#x5217;&#xFF09;&#x7684;&#x968F;&#x673A;&#x591A;&#x7EF4;&#x6D6E;&#x70B9;&#x578B;&#x6570;&#x636E;&#xFF08;&#x4E8C;&#x7EF4;&#xFF09;&#xFF0C;rand&#x56FA;&#x5B9A;&#x533A;&#x95F4;0.0 ~ 1.0</span>
arr = np.random.rand(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
print(arr)
print(type(arr))

<span class="hljs-comment"># &#x751F;&#x6210;&#x6307;&#x5B9A;&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;&#xFF08;3&#x884C;4&#x5217;&#xFF09;&#x7684;&#x968F;&#x673A;&#x591A;&#x7EF4;&#x6574;&#x578B;&#x6570;&#x636E;&#xFF08;&#x4E8C;&#x7EF4;&#xFF09;&#xFF0C;randint()&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x533A;&#x95F4;&#xFF08;-1, 5&#xFF09;</span>
arr = np.random.randint(-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, size = (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)) <span class="hljs-comment"># &apos;size=&apos;&#x53EF;&#x7701;&#x7565;</span>
print(arr)
print(type(arr))

<span class="hljs-comment"># &#x751F;&#x6210;&#x6307;&#x5B9A;&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;&#xFF08;3&#x884C;4&#x5217;&#xFF09;&#x7684;&#x968F;&#x673A;&#x591A;&#x7EF4;&#x6D6E;&#x70B9;&#x578B;&#x6570;&#x636E;&#xFF08;&#x4E8C;&#x7EF4;&#xFF09;&#xFF0C;uniform()&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x533A;&#x95F4;&#xFF08;-1, 5&#xFF09;</span>
arr = np.random.uniform(-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, size = (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)) <span class="hljs-comment"># &apos;size=&apos;&#x53EF;&#x7701;&#x7565;</span>
print(arr)
print(type(arr))

print(<span class="hljs-string">&apos;&#x7EF4;&#x5EA6;&#x4E2A;&#x6570;: &apos;</span>, arr.ndim)
print(<span class="hljs-string">&apos;&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;: &apos;</span>, arr.shape)
print(<span class="hljs-string">&apos;&#x6570;&#x636E;&#x7C7B;&#x578B;: &apos;</span>, arr.dtype)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[[ 0.09371338  0.06273976  0.22748452  0.49557778]
 [ 0.30840042  0.35659161  0.54995724  0.018144  ]
 [ 0.94551493  0.70916088  0.58877255  0.90435672]]
&lt;class &apos;numpy.ndarray&apos;&gt;

[[ 1  3  0  1]
 [ 1  4  4  3]
 [ 2  0 -1 -1]]
&lt;class &apos;numpy.ndarray&apos;&gt;

[[ 2.25275308  1.67484038 -0.03161878 -0.44635706]
 [ 1.35459097  1.66294159  2.47419548 -0.51144655]
 [ 1.43987571  4.71505054  4.33634358  2.48202309]]
&lt;class &apos;numpy.ndarray&apos;&gt;

&#x7EF4;&#x5EA6;&#x4E2A;&#x6570;:  2
&#x7EF4;&#x5EA6;&#x5927;&#x5C0F;:  (3, 4)
&#x6570;&#x636E;&#x7C7B;&#x578B;:  float64
</code></pre>
<blockquote>
<h2 id="ndarray&#x7684;&#x5E8F;&#x5217;&#x521B;&#x5EFA;">ndarray&#x7684;&#x5E8F;&#x5217;&#x521B;&#x5EFA;</h2>
</blockquote>
<h4 id="1-nparraycollection">1. <code>np.array(collection)</code></h4>
<blockquote>
<p>collection &#x4E3A; &#x5E8F;&#x5217;&#x578B;&#x5BF9;&#x8C61;(list)&#x3001;&#x5D4C;&#x5957;&#x5E8F;&#x5217;&#x5BF9;&#x8C61;(list of list)&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># list&#x5E8F;&#x5217;&#x8F6C;&#x6362;&#x4E3A; ndarray</span>
lis = range(<span class="hljs-number">10</span>)
arr = np.array(lis)

print(arr)            <span class="hljs-comment"># ndarray&#x6570;&#x636E;</span>
print(arr.ndim)        <span class="hljs-comment"># &#x7EF4;&#x5EA6;&#x4E2A;&#x6570;</span>
print(arr.shape)    <span class="hljs-comment"># &#x7EF4;&#x5EA6;&#x5927;&#x5C0F;</span>

<span class="hljs-comment"># list of list&#x5D4C;&#x5957;&#x5E8F;&#x5217;&#x8F6C;&#x6362;&#x4E3A;ndarray</span>
lis_lis = [range(<span class="hljs-number">10</span>), range(<span class="hljs-number">10</span>)]
arr = np.array(lis_lis)

print(arr)            <span class="hljs-comment"># ndarray&#x6570;&#x636E;</span>
print(arr.ndim)        <span class="hljs-comment"># &#x7EF4;&#x5EA6;&#x4E2A;&#x6570;</span>
print(arr.shape)    <span class="hljs-comment"># &#x7EF4;&#x5EA6;&#x5927;&#x5C0F;</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># list&#x5E8F;&#x5217;&#x8F6C;&#x6362;&#x4E3A; ndarray</span>
[<span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>]
<span class="hljs-number">1</span>
(<span class="hljs-number">10</span>,)

<span class="hljs-comment"># list of list&#x5D4C;&#x5957;&#x5E8F;&#x5217;&#x8F6C;&#x6362;&#x4E3A; ndarray</span>
[[<span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>]
 [<span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>]]
<span class="hljs-number">2</span>
(<span class="hljs-number">2</span>, <span class="hljs-number">10</span>)
</code></pre>
<h4 id="2-npzeros">2. <code>np.zeros()</code></h4>
<blockquote>
<p>&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x5168;0&#x6570;&#x7EC4;&#x3002;&#x6CE8;&#x610F;&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5143;&#x7EC4;&#xFF0C;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#xFF0C;&#x5982;(3, 4)&#x3002;</p>
</blockquote>
<h4 id="3-npones">3. <code>np.ones()</code></h4>
<blockquote>
<p>&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x5168;1&#x6570;&#x7EC4;&#x3002;&#x6CE8;&#x610F;&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5143;&#x7EC4;&#xFF0C;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#xFF0C;&#x5982;(3, 4)&#x3002;</p>
</blockquote>
<h4 id="4-npempty">4. <code>np.empty()</code></h4>
<blockquote>
<p>&#x521D;&#x59CB;&#x5316;&#x6570;&#x7EC4;&#xFF0C;&#x4E0D;&#x662F;&#x603B;&#x662F;&#x8FD4;&#x56DE;&#x5168;0&#xFF0C;&#x6709;&#x65F6;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x672A;&#x521D;&#x59CB;&#x7684;&#x968F;&#x673A;&#x503C;&#xFF08;&#x5185;&#x5B58;&#x91CC;&#x7684;&#x968F;&#x673A;&#x503C;&#xFF09;&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF08;2&#x3001;3&#x3001;4&#xFF09;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># np.zeros</span>
zeros_arr = np.zeros((<span class="hljs-number">3</span>, <span class="hljs-number">4</span>))

<span class="hljs-comment"># np.ones</span>
ones_arr = np.ones((<span class="hljs-number">2</span>, <span class="hljs-number">3</span>))

<span class="hljs-comment"># np.empty</span>
empty_arr = np.empty((<span class="hljs-number">3</span>, <span class="hljs-number">3</span>))

<span class="hljs-comment"># np.empty &#x6307;&#x5B9A;&#x6570;&#x636E;&#x7C7B;&#x578B;</span>
empty_int_arr = np.empty((<span class="hljs-number">3</span>, <span class="hljs-number">3</span>), int)

print(<span class="hljs-string">&apos;------zeros_arr-------&apos;</span>)
print(zeros_arr)

print(<span class="hljs-string">&apos;\n------ones_arr-------&apos;</span>)
print(ones_arr)

print(<span class="hljs-string">&apos;\n------empty_arr-------&apos;</span>)
print(empty_arr)

print(<span class="hljs-string">&apos;\n------empty_int_arr-------&apos;</span>)
print(empty_int_arr)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">------zeros_arr-------
[[ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]]

------ones_arr-------
[[ <span class="hljs-number">1.</span>  <span class="hljs-number">1.</span>  <span class="hljs-number">1.</span>]
 [ <span class="hljs-number">1.</span>  <span class="hljs-number">1.</span>  <span class="hljs-number">1.</span>]]

------empty_arr-------
[[ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]]

------empty_int_arr-------
[[<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]
 [<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]
 [<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]]
</code></pre>
<h4 id="5-nparange-&#x548C;-reshape">5. <code>np.arange()</code> &#x548C; <code>reshape()</code></h4>
<blockquote>
<p>arange() &#x7C7B;&#x4F3C; python &#x7684; range() &#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4E00;&#x7EF4; ndarray &#x6570;&#x7EC4;&#x3002;</p>
<p>reshape() &#x5C06; &#x91CD;&#x65B0;&#x8C03;&#x6574;&#x6570;&#x7EC4;&#x7684;&#x7EF4;&#x6570;&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF08;5&#xFF09;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># np.arange()</span>
arr = np.arange(<span class="hljs-number">15</span>) <span class="hljs-comment"># 15&#x4E2A;&#x5143;&#x7D20;&#x7684; &#x4E00;&#x7EF4;&#x6570;&#x7EC4;</span>
print(arr)
print(arr.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>)) <span class="hljs-comment"># 3x5&#x4E2A;&#x5143;&#x7D20;&#x7684; &#x4E8C;&#x7EF4;&#x6570;&#x7EC4;</span>
print(arr.reshape(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>)) <span class="hljs-comment"># 1x3x5&#x4E2A;&#x5143;&#x7D20;&#x7684; &#x4E09;&#x7EF4;&#x6570;&#x7EC4;</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[ <span class="hljs-number">0</span>  <span class="hljs-number">1</span>  <span class="hljs-number">2</span>  <span class="hljs-number">3</span>  <span class="hljs-number">4</span>  <span class="hljs-number">5</span>  <span class="hljs-number">6</span>  <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span> <span class="hljs-number">10</span> <span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span>]

[[ <span class="hljs-number">0</span>  <span class="hljs-number">1</span>  <span class="hljs-number">2</span>  <span class="hljs-number">3</span>  <span class="hljs-number">4</span>]
 [ <span class="hljs-number">5</span>  <span class="hljs-number">6</span>  <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span>]
 [<span class="hljs-number">10</span> <span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span>]]

[[[ <span class="hljs-number">0</span>  <span class="hljs-number">1</span>  <span class="hljs-number">2</span>  <span class="hljs-number">3</span>  <span class="hljs-number">4</span>]
  [ <span class="hljs-number">5</span>  <span class="hljs-number">6</span>  <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span>]
  [<span class="hljs-number">10</span> <span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span>]]]
</code></pre>
<h4 id="6-nparange-&#x548C;-randomshuffle">6. <code>np.arange()</code> &#x548C; <code>random.shuffle()</code></h4>
<blockquote>
<p>random.shuffle() &#x5C06;&#x6253;&#x4E71;&#x6570;&#x7EC4;&#x5E8F;&#x5217;&#xFF08;&#x7C7B;&#x4F3C;&#x4E8E;&#x6D17;&#x724C;&#xFF09;&#x3002;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF08;6&#xFF09;&#xFF1A;</p>
<pre><code class="lang-python">arr = np.arange(<span class="hljs-number">15</span>)
print(arr)

np.random.shuffle(arr)
print(arr)
print(arr.reshape(<span class="hljs-number">3</span>,<span class="hljs-number">5</span>))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[ <span class="hljs-number">0</span>  <span class="hljs-number">1</span>  <span class="hljs-number">2</span>  <span class="hljs-number">3</span>  <span class="hljs-number">4</span>  <span class="hljs-number">5</span>  <span class="hljs-number">6</span>  <span class="hljs-number">7</span>  <span class="hljs-number">8</span>  <span class="hljs-number">9</span> <span class="hljs-number">10</span> <span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span>]

[ <span class="hljs-number">5</span>  <span class="hljs-number">8</span>  <span class="hljs-number">1</span>  <span class="hljs-number">7</span>  <span class="hljs-number">4</span>  <span class="hljs-number">0</span> <span class="hljs-number">12</span>  <span class="hljs-number">9</span> <span class="hljs-number">11</span>  <span class="hljs-number">2</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span> <span class="hljs-number">10</span>  <span class="hljs-number">3</span>  <span class="hljs-number">6</span>]

[[ <span class="hljs-number">5</span>  <span class="hljs-number">8</span>  <span class="hljs-number">1</span>  <span class="hljs-number">7</span>  <span class="hljs-number">4</span>]
 [ <span class="hljs-number">0</span> <span class="hljs-number">12</span>  <span class="hljs-number">9</span> <span class="hljs-number">11</span>  <span class="hljs-number">2</span>]
 [<span class="hljs-number">13</span> <span class="hljs-number">14</span> <span class="hljs-number">10</span>  <span class="hljs-number">3</span>  <span class="hljs-number">6</span>]]
</code></pre>
<blockquote>
<h2 id="ndarray&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;">ndarray&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
</blockquote>
<h4 id="1-dtype&#x53C2;&#x6570;">1. <code>dtype</code>&#x53C2;&#x6570;</h4>
<blockquote>
<p>&#x6307;&#x5B9A;&#x6570;&#x7EC4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x7C7B;&#x578B;&#x540D;+&#x4F4D;&#x6570;&#xFF0C;&#x5982;float64, int32</p>
</blockquote>
<h4 id="2-astype&#x65B9;&#x6CD5;">2. <code>astype</code>&#x65B9;&#x6CD5;</h4>
<blockquote>
<p>&#x8F6C;&#x6362;&#x6570;&#x7EC4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF08;1&#x3001;2&#xFF09;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x521D;&#x59CB;&#x5316;3&#x884C;4&#x5217;&#x6570;&#x7EC4;&#xFF0C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x4E3A;float64</span>
zeros_float_arr = np.zeros((<span class="hljs-number">3</span>, <span class="hljs-number">4</span>), dtype=np.float64)
print(zeros_float_arr)
print(zeros_float_arr.dtype)

<span class="hljs-comment"># astype&#x8F6C;&#x6362;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x5C06;&#x5DF2;&#x6709;&#x7684;&#x6570;&#x7EC4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;int32</span>
zeros_int_arr = zeros_float_arr.astype(np.int32)
print(zeros_int_arr)
print(zeros_int_arr.dtype)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[[ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]
 [ <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>  <span class="hljs-number">0.</span>]]
float64

[[<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]
 [<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]
 [<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>]]
int32
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2017-03-12 22:35:01&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part02/2.html" class="navigation navigation-prev " aria-label="Previous page: 二、科学计算工具NumPy"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part02/2.2.html" class="navigation navigation-next " aria-label="Next page: ndarray的矩阵处理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python数据分析课程讲义 -->
</html>
